package com.qiang.test08;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws DocumentException {
        //获取解析器
        SAXReader saxReader = new SAXReader();
        //加载到内存中
        Document read = saxReader.read(new File("day17/src/com/qiang/test08/person.xml"));
        //获取根标签
        Element rootElement = read.getRootElement();

        //获取指定的子标签
        List<Element> elements = rootElement.elements("person");

        ArrayList<Student> list = new ArrayList<>();

        for (Element element : elements) {

            //获取id属性
            Attribute id = element.attribute("id");
            String value = id.getValue();

            //获取名字标签
            Element name = element.element("name");
            String text = name.getText();

            Element age = element.element("age");
            String text1 = age.getText();

            Student student = new Student(value, text, Integer.parseInt(text1));
            list.add(student);
        }
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
